/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package strategies;

import common.Application;
import common.ParamStorage;
import dao.Entity;
import dao.EntityDAO;
import database.DatabaseException;
import formatters.EditFormatter;
import formatters.Formatter;
import html.InputType;
import html.XHtmlFormatter;
import java.util.Map;
import localization.Localization;
import parsers.InputParser;
import presentation.PresentationElement;

/**
 *
 * @author Alejandro
 */
public class EditEntityPageStrategy implements Strategy {

    @Override
    public void execute(WebContext ctx) {
        XHtmlFormatter provider = ctx.getWebFormatter();
        ParamStorage storage = ctx.getParamStorage();
        provider.startDiv("", "operationResult");
        //provider.writeP("center", this.getClass().getName());
        boolean tableSelection = storage.hasParam("table-selection") && Application.getInstance().getTemplate(Short.parseShort(storage.getParam("table-selection"))) != null;
        boolean add = storage.hasParam("add");
        boolean validateAdd = storage.hasParam("validate-add");
        boolean edit = storage.hasParam("edit");
        boolean validateEdit = storage.hasParam("validate-edit");

        Entity e = null;
        if (add || validateAdd) {
            if (tableSelection) {
                e = Entity.newInstance(Short.parseShort(storage.getParam("table-selection")));
                if (validateAdd) {
                    InputParser parser = new InputParser(storage);
                    for (PresentationElement pe : e.getPresentationCollection()) {
                        pe.setValue(parser.parse(pe));
                    }
                }
            } else {
                provider.putError(Localization.getString("NoTableSelected"));
            }
        }
        if (edit || validateEdit) {
            Map<String, String> rows = storage.getRows();
            String key = "";
            for (String s : rows.keySet()) {
                if (rows.get(s).equals("on")) {
                    key = s;
                    break;
                }
            }

            if (validateEdit) {
                key = storage.getParam("save_primary_key");
            }

            if (!key.isEmpty()) {
                try {
                    if (tableSelection) {
                        EntityDAO dao = new EntityDAO(Short.parseShort(storage.getParam("table-selection")));
                        e = (Entity) dao.findByPrimaryKey(key);
                        provider.writeInput(InputType.hidden, "save_primary_key", "save_primary_key", "", key);
                        if (validateEdit) {
                            InputParser parser = new InputParser(storage);
                            for (PresentationElement pe : e.getPresentationCollection()) {
                                pe.setValue(parser.parse(pe));
                            }
                        }
                    } else {
                        provider.putError(Localization.getString("NoTableSelected"));
                    }
                } catch (DatabaseException ex) {
                }
            } else {
                provider.putError(Localization.getString("NoRowSelected"));
            }
        }

        if (e != null) {
            Formatter f = new EditFormatter();
            Iterable<PresentationElement> presentations = e.getPresentationCollection();
            for (PresentationElement pe : presentations) {
                if (!e.isPrimaryKeyPresentation(pe)) {
                    provider.startDiv("", "editBlock");
                    provider.echo(f.format(pe));
                    provider.endDiv();
                }
            }
        }
        provider.endDiv();
    }
}
